package com.neu.questionnaire_system.service.impl;

import com.neu.questionnaire_system.beans.ResponseEntity;
import com.neu.questionnaire_system.entity.Tenant;
import com.neu.questionnaire_system.entity.User;
import com.neu.questionnaire_system.mapper.*;
import com.neu.questionnaire_system.service.RegisterService;
import com.neu.questionnaire_system.util.Constants;
import com.neu.questionnaire_system.util.UUIDGenerator;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.Map;

/**
 * @author libo
 * @version 2022-11-2
 * 注册服务器
 */
@Service
public class RegisterServiceImpl implements RegisterService {

    @Autowired
    private RegisterMapper registerMapper;

    @Autowired
    private TenantMapper tenantMapper;

    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private AnswerMapper answerMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public ResponseEntity registerType(Map<String,Object> map) {

        ResponseEntity responseEntity = new ResponseEntity();
        responseEntity.setCode(Constants.SUCCESS_CODE);
        if ("租户".equals(map.get("type").toString())){
            responseEntity.setData(registerMapper.registerTenant());
        }else if ("用户".equals(map.get("type").toString())){
            responseEntity.setData(registerMapper.registerUser());
        }else if ("答者".equals(map.get("type").toString())){

        }
        responseEntity.setMessage(Constants.REGISTER_MESSAGE);
        return responseEntity;
    }

    @Override
    public ResponseEntity registerTenant(Map<String, Object> map) throws InvocationTargetException, IllegalAccessException {

        ResponseEntity responseEntity = new ResponseEntity();
        if(map.get("tenantName") != null){
            //判断租户是否存在，并返回一个值，用接收的值判断存在与否
            if (tenantMapper.findTidByName(map.get("tenantName").toString())!=null){
                responseEntity.setCode(Constants.FAIL_CODE);
                responseEntity.setMessage(Constants.TENANT_REPEAT_MESSAGE);//表示租户已经存在不能创建
                return responseEntity;
            }
        }
        String id = adminMapper.findIdByName(map.get("adminName").toString());//查询管理员的id
        map.put("id",id);//将管理员id加入到map中
        Double fee = Double.valueOf(0);
        map.put("fee",fee);//初始化费用为0.0
        String tid = UUIDGenerator.getOneUUID();//这个工具类不要导入错了，我自己写的在util包下
        Tenant tenant = new Tenant();
        map.put("tid",tid);//将生成的tid加入map中。
        BeanUtils.populate(tenant,map);//用map中的元素封装tenant
        int res = tenantMapper.insert(tenant);
        responseEntity.setCode(Constants.SUCCESS_CODE);//封装状态码到返回的对象
        responseEntity.setMessage(Constants.ADD_MESSAGE);//封装状态消息到返回的对象
        return responseEntity;

    }


    /**
     * created by libo 重置密码查询是否存在
     * @param map 包含type和name
     * @return 统一对象
     */
    public ResponseEntity resetQueryService(Map<String,Object> map){
        ResponseEntity responseEntity = new ResponseEntity();
        if ("用户".equals(map.get("type").toString())){
            int res = userMapper.queryUserIsEmpty(map.get("name").toString());
            if (res>0){
                responseEntity.setCode(Constants.SUCCESS_CODE);
                responseEntity.setMessage(Constants.SELECT_MESSAGE);
                return responseEntity;
            }
        }else if ("租户".equals(map.get("type").toString())){
            String res = tenantMapper.findTidByName(map.get("name").toString());
            if (res!=null){
                responseEntity.setCode(Constants.SUCCESS_CODE);
                responseEntity.setMessage(Constants.SELECT_MESSAGE);
                return responseEntity;
            }
        }else {
            int res =  answerMapper.queryAnswerIsEmpty(map.get("name").toString());
            if (res>0){
                responseEntity.setCode(Constants.SUCCESS_CODE);
                responseEntity.setMessage(Constants.SELECT_MESSAGE);
                return responseEntity;
            }
        }
        responseEntity.setCode(Constants.FAIL_CODE);
        responseEntity.setMessage(Constants.SELECT_FAIL_MESSAGE);
        return responseEntity;
    }

    /**
     * created by libo 重置密码
     * @param map 包含登录类型type
     * @return 统一的对象
     */
    public ResponseEntity resetService(Map<String,Object> map){
        ResponseEntity responseEntity = new ResponseEntity();
        if ("用户".equals(map.get("type").toString())){
            int res = registerMapper.resetUserInfo(map.get("name").toString(),map.get("password").toString());
            if (res>0){
                responseEntity.setCode(Constants.SUCCESS_CODE);
                responseEntity.setMessage(Constants.SELECT_MESSAGE);
                return responseEntity;
            }
        }else if ("租户".equals(map.get("type").toString())){
            int res = registerMapper.resetTenantInfo(map.get("name").toString(),map.get("password").toString());
            if (res>0){
                responseEntity.setCode(Constants.SUCCESS_CODE);
                responseEntity.setMessage(Constants.SELECT_MESSAGE);
                return responseEntity;
            }
        }else {
            int res = registerMapper.resetAnswerInfo(map.get("name").toString(),map.get("password").toString());
            if (res>0){
                responseEntity.setCode(Constants.SUCCESS_CODE);
                responseEntity.setMessage(Constants.SELECT_MESSAGE);
                return responseEntity;
            }
        }
        responseEntity.setCode(Constants.FAIL_CODE);
        responseEntity.setMessage(Constants.SELECT_FAIL_MESSAGE);
        return responseEntity;
    }



}
